Fix set comprehension in per_class_scorer causing wrong overall metrics#567
Open
Chessing234 wants to merge 1 commit intoallenai:mainfrom
Open
Fix set comprehension in per_class_scorer causing wrong overall metrics#567Chessing234 wants to merge 1 commit intoallenai:mainfrom
Chessing234 wants to merge 1 commit intoallenai:mainfrom
Conversation
The overall precision/recall/F1 computation uses set comprehensions
({v for ...}) instead of list comprehensions ([v for ...]). Sets
deduplicate values, so if two entity types share the same TP/FP/FN
count, only one copy is summed — silently producing incorrect metrics.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
get_metric()inPerClassScorercomputes overall precision/recall/F1 by summing per-label TP, FP, and FN counts. The comprehensions use{v for ...}(set) instead of[v for ...](list), which deduplicates values before summing.Bug: If two entity types share the same count (e.g.,
DISEASE: 3, CHEMICAL: 3), the set{3}collapses them into a single3, and the sum is3instead of the correct6. This silently produces incorrect overall metrics whenever any two labels happen to have equal counts.Fix: Replace set comprehensions with list comprehensions on lines 72, 75, and 78 (
{→[,}→]).Why the existing test doesn't catch this: The test in
test_per_class_scorer.pyuses a scenario where every per-label count is 0 or 1 — all values are already distinct, so deduplication has no effect.Test plan
TP = {A: 3, B: 3}, the old code sums to 3 (wrong), the new code sums to 6 (correct)🤖 Generated with Claude Code